/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.kai.zhouzhi.dao.impl;

import com.kai.zhouzhi.dao.DaoHelper;
import com.kai.zhouzhi.entity.User;
import com.kai.zhouzhi.util.EntityManagerUtil;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Kenny Lin
 */
public class DaoHelperImpl implements DaoHelper{

    public void create(Object obj) {
         EntityManager em = null;
        try {
            em = EntityManagerUtil.getEntityManager();
            em.getTransaction().begin();
            em.persist(obj);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Object obj) {
        EntityManager em = null;
        try {
            em = EntityManagerUtil.getEntityManager();
            em.getTransaction().begin();
            em.merge(obj);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Object obj) {
        EntityManager em = null;
        try {
            em = EntityManagerUtil.getEntityManager();
            em.getTransaction().begin();
            em.remove(obj);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public <T> List<T> findEntities(Class<T> classType) {
        return findEntities(classType, true, -1, -1);
    }

    public <T> List<T> findEntities(Class<T> classType, int maxResults, int firstResult) {
        return findEntities(classType, false, maxResults, firstResult);
    }

    private <T> List<T> findEntities(Class<T> classType, boolean all, int maxResults, int firstResult) {
        EntityManager em = null;
        try {
            em = EntityManagerUtil.getEntityManager();
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(classType));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public <T> T find(Class<T> classType, Object key) {
        EntityManager em = null;
        try {
            em = EntityManagerUtil.getEntityManager();
            return em.find(classType, key);
        } finally {
            em.close();
        }
    }

    public <T> int getCount(Class<T> classType) {
        EntityManager em = null;
        try {
            em = EntityManagerUtil.getEntityManager();
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T> rt = cq.from(classType);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public static void main(String[] args){
        DaoHelper daoHelper = new DaoHelperImpl();
        User user = new User();
        user.setUserName("test");
        user.setUserPwd("test");
        user.setUserType("admin");
        daoHelper.create(user);
        
        System.out.println(daoHelper.findEntities(User.class).size());
    }
    
}
